#include <iostream>

#include <unistd.h>
#include <sys/wait.h>

#include "server.h"
#include "client.h"

int main(int argc, char *argv[]) {
    pid_t pid, spid, cpid;

    if (argc < 2) {
        std::cerr << "[ E ] must specifal a array name!" << std::endl;
        return 1;
    }

    if ((spid = fork()) == 0) {
        smain(argv[1]);
        //RShareMemArray arr(argv[1], 100, sizeof(data_t));
        //smain(arr);
    } else {
        if ((cpid = fork()) == 0) {
            cmain(argv[1]);
            //RShareMemArray arr(argv[1], 100, sizeof(data_t));
            //cmain(arr);
        } else {
            int status, i;

            for (i = 0; i < 2; i++) {
                pid = wait(&status);

                if (!WIFEXITED(status)) {
                    std::cerr << "[ E ] Child " << pid << " did NOT exit successfully！(server: " << spid << ", client: " << cpid << ")" << std::endl;

                    if (WIFSIGNALED(status))
                        std::cerr << "[ E ] Killed by signal: " << WTERMSIG(status) << std::endl;
                }
            }

            std::cout << "[ I ] Finished." << std::endl;
        }
    }

    return 0;
}
